package com.example.wallpaper.photoapi;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.wallpaper.model.Wallpaper;
import com.example.wallpaper.tools.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class DmoeCatch {

    private static final String TAG = "DmoeCatch";

    private static final String URL ="https://www.dmoe.cc/random.php?return=json";

    private HttpUtil mHttpUtil;

    //定义接口返回的数据格式
    private static class JsonCol{
        private static String code ="code";
        private static String imgUrl ="imgurl";
        private static String width ="width";
        private static String height ="height";
    }

    public DmoeCatch(){
        mHttpUtil = new HttpUtil();
    }

    public void getWallpaper(Handler handler,int position){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //第一次网络请求，请求图片信息
                String json = mHttpUtil.sendRequest(URL);
                if(json!=null){
                    //把json字符串转换成JsonObject对象
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        int code = jsonObject.getInt(JsonCol.code);
                        Log.i(TAG,"请求返回的数据："+json);
                        if(code==200){//表示图片信息正常返回
                            String imgUrl = jsonObject.getString(JsonCol.imgUrl);
                            //第二次网络请求，请求图片内容
                            byte[] bytes = mHttpUtil.sendRequestByByteArray(imgUrl);
                            if(bytes!=null){
                                //图片转换成缩略图
                                BitmapFactory.Options options = new BitmapFactory.Options();
                                options.inSampleSize = 10;
                                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,options);
                                Wallpaper wallpaper = new Wallpaper();
                                wallpaper.setImgUrl(imgUrl);
                                wallpaper.setBitmap(bitmap);
                                //子线程返回数据到主线程
                                Message msg = new Message();
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("args",wallpaper);
                                bundle.putInt("position",position);
                                msg.setData(bundle);
                                //子线程发送数据
                                handler.sendMessage(msg);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public void getWallpaperView(Handler handler,String imgUrl){
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] bytes = mHttpUtil.sendRequestByByteArray(imgUrl);
                //子线程返回数据到主线程
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putByteArray("args",bytes);
                msg.setData(bundle);
                //子线程发送数据
                handler.sendMessage(msg);
            }
        }).start();
    }
}
